package com.think.teaching.controller;

import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.News;
import com.think.teaching.service.impl.NewsServiceImpl;
import com.think.teaching.service.impl.TagServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@Api(tags = "新闻接口文档")
public class NewsController extends BaseController {

	private final NewsServiceImpl newsService;
	private final TagServiceImpl tagService ;

	public NewsController(NewsServiceImpl newsService, TagServiceImpl tagService) {
		this.newsService = newsService;
		this.tagService =tagService;
	}

	@ApiOperation("添加新闻")
	@PostMapping("admin/news")
	public Result AddNewNews(@RequestBody() News data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			
			News news;
			//文章标题不得重复
			if(newsService.GetNewsCountByTitle(data.getTitle())>0)
				return Result.failure(ResultCode.ARTICLE_TITLE_ALREADY_EXIST);
			
			try {
				news = newsService.AddNews(data);
			} catch (Throwable e) {				
				e.printStackTrace();
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR,e.getLocalizedMessage());
			}
			if (news != null) {
				return Result.success(news);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新新闻")
	@RequestMapping(value = "admin/news", method = RequestMethod.PUT)
	public Result UpdateNews(@RequestBody() News data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			News news = null;
			try {
				news = newsService.UpdateNews(data);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (news != null) {
				return Result.success(news);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("删除新闻")
	@RequestMapping(value = "admin/news/{id}", method = RequestMethod.DELETE)
	public Result DeleteNews(@PathVariable("id") Integer id) {
		News news = newsService.getById(id);
		if (news == null) {
			return Result.failure(ResultCode.ARTICLE_NOT_EXIST);
		} else {
			if (newsService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("获取新闻详情")
	@RequestMapping(value = "admin/news/{id}", method = RequestMethod.GET)
	public Result GetNews(@PathVariable("id") Integer id) {
		News news = newsService.GetNews(id,false);
		if (news == null) {
			return Result.failure(ResultCode.ARTICLE_NOT_EXIST);
		} else {

			return Result.success(news);
		}
	}
	
	@ApiOperation("获取新闻详情")
	@RequestMapping(value = "open/news/{id}", method = RequestMethod.GET)
	public Result UserGetNews(@PathVariable("id") Integer id) {
		News news = newsService.GetNews(id,true);
		if (news == null) {
			return Result.failure(ResultCode.ARTICLE_NOT_EXIST);
		} else {

			return Result.success(news);
		}
	}
	
	@ApiOperation("获取新闻详情")
	@RequestMapping(value = "open/news/recommend", method = RequestMethod.GET)
	public Result UserGetRecommendNews(@RequestParam("count") Integer count) {
		List<News> news = newsService.GetRecommendNews(count);
		return Result.success(news);
	}

	@ApiOperation("管理员修改文章状态")
	@RequestMapping(value = "admin/news/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateNewsState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		News news = newsService.getById(id);
		if (news == null) {
			return Result.failure(ResultCode.ARTICLE_NOT_EXIST);
		} else {
			try {
				news = newsService.SetNewsState(id, state);
				return Result.success(news);
			} catch (Exception e) {
				// TODO: handle exception
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索文章")
	@RequestMapping(value = "user/news/search", method = RequestMethod.GET)
	public Result UserSearchNews(@RequestParam("key") String key) {
		return Result.success(newsService.ListNewsSearch(key, true));
	}

	@ApiOperation("管理员搜索文章")
	@RequestMapping(value = "admin/news/search", method = RequestMethod.GET)
	public Result AdminSearchNews(@RequestParam("key") String key) {
		return Result.success(newsService.ListNewsSearch(key, false));
	}

	@ApiOperation("管理员获取文章列表")
	@RequestMapping(value = "admin/newses/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListNews(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(value = "tag", defaultValue = "-1") Integer tag,
			@RequestParam(value = "dataType", defaultValue = "-1") Integer dataType,
			@RequestParam(value = "type", defaultValue = "-1") Integer type) {
		return Result.success(newsService.ListNewsWithTagAndType(page,count, type, tag, false,dataType));
	}

	@ApiOperation("用户获取文章列表")
	@RequestMapping(value = "user/newses/{page}/{count}", method = RequestMethod.GET)
	public Result UserListNews(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(value = "tag", defaultValue = "-1") Integer tag,
			@RequestParam(value = "dataType", defaultValue = "-1") Integer dataType,
			@RequestParam(value = "type", defaultValue = "-1") Integer type) {
		if(!tagService.CheckTagExist(tag))
			return Result.failure(ResultCode.TAG_NOT_EXIST);
		else
			return Result.success(newsService.ListNewsWithTagAndType(page,count, type, tag, true,dataType));
	}
}
